<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml/" version="1.0">

  <!-- 
    transform the JUnit-like XML test results to a nice xhtml representation.
  
    Authors: Jan Willem Boer <janwillem@jwenr.nl>
  -->
  
  <xsl:output method="html"/>

  <xsl:param name="showall"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>CoUnit test results</title>
        <style>
          html {
            font-family: 'trebuchet ms', sans-serif;
            font-size: 10pt;
          }
          table.testsuite {
            border: 2px solid black;
            background-color: #eee;
          }
          .passed, .failed {
            padding: 2px;
          }
          .passed {
            background-color: green;
            color: white;
          }
          .failed {
            background-color: red;
          }
          td {
            vertical-align: top;
          }
          
          /* failure details */
          .testcase-label {
            padding-top: 5px;
            padding-right: 10px;
            padding-bottom: 5px;
            display: block;
            text-decoration: none;
            color: black;
          }
          .testcase-label:hover {
            text-decoration: underline;
          }
          
          .failure {
            background-color: white;
            border: 1px solid silver;
            padding: 5px;
          }
          .message {
            font-style: italic;
          }
          .expected-header, .received-header {
            margin-top: 9px;
            font-style: italic;
            border-bottom: 1px solid silver;
          }
          .element-start, .element-close {
            color: blue;
          }
          .element-content {
            margin-left: 10px;
          }
          .expected, .received {
            font-family: 'courier new';
          }
          .processing-instruction {
            color: #a0a;
          }
          .comment {
            color: #0a0;
          }
          
         </style>
      </head>
      <body>
        <xsl:apply-templates/>
        <p id="showall">
          <xsl:choose>
            <xsl:when test="$showall='true'">
              <a href="?counit-showall=false">Show only failed tests.</a>
            </xsl:when>
            <xsl:otherwise>
              <a href="?counit-showall=true">Show all tests, not only failed ones.</a>
            </xsl:otherwise>
          </xsl:choose>
        </p>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="/testsuite" priority="15">
    <xsl:apply-templates select="." mode="ts"/>
  </xsl:template>

  <xsl:template match="testsuite[@tests=0]" priority="10">
    <!-- no test results in this suite, probably only 1 test is being performed -->
    <!-- ignore -->
  </xsl:template>

  <xsl:template match="testsuite[parent::testsuite]" priority="5">
    <tr>
      <td colspan="2">
        <xsl:apply-templates select="." mode="ts"/>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="testsuite" mode="ts">
    <xsl:variable name="testsuite-name">
      <xsl:choose>
        <xsl:when test="string-length(@name) = 0">
          <xsl:value-of select="@package"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@name"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <table class="testsuite">
      <tr>
        <th colspan="2">
          <xsl:value-of select="$testsuite-name"/>
        </th>
      </tr>
      <tr>
        <td colspan="2">
          <xsl:choose>
            <xsl:when test="@failures = 0">
              <div class="passed">Passed (<xsl:value-of select="@tests"/> tests)</div>
            </xsl:when>
            <xsl:otherwise>
              <div class="failed">Failed (<xsl:value-of select="@failures"/> of <xsl:value-of
                  select="@tests"/> tests)</div>
            </xsl:otherwise>
          </xsl:choose>
        </td>
      </tr>
      <xsl:if test="@failures != 0 or $showall = 'true'">
        <xsl:apply-templates/>
      </xsl:if>
    </table>
  </xsl:template>

  <xsl:template match="testcase">
    <xsl:if test="failure or $showall='true'">
      <tr>
        <td>
          <a class="testcase-label" href="{@name}" title="Perform only this test">
            <xsl:value-of select="@name"/>
          </a>
        </td>
        <td>
          <xsl:apply-templates/>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <xsl:template match="failure">
    <div class="failure">
      <div class="message"><xsl:value-of select="@message"/></div>
      <xsl:apply-templates select="node()" mode="copy"/>
    </div>
  </xsl:template>

  <!-- copy everything else -->
  <xsl:template match="node()|@*" mode="copy">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="node()" mode="copy"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
